package com.faithnmn.learnenglishbc.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.ocpsoft.prettytime.PrettyTime;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

public class CommonUtil {
	public static void writeToFile(String content, String filename,boolean isAppend) {
		String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Logs/HocTiengAnh30Phut" ;
		File file = new File(path);
		try {
			if(file.mkdirs()||file.exists()){
				path+="/"+filename;
				BufferedWriter out = new BufferedWriter(new FileWriter(path,isAppend));
				out.write(content);
				out.close();
			}
			
		} catch (Exception e) {
			System.out.println("Exception " + e.toString());
		}

	}
	
	public static String convertFBUnixtime(String fbUnixtime){
		Date date = new Date(Long.parseLong(fbUnixtime)*1000L); // *1000 is to convert seconds to milliseconds
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd-MM-yyyy"); // the format of your date
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));
		return sdf.format(date);
	}
	
	public static String getPrettyTime(long unixTime){
		PrettyTime pt= new PrettyTime();
		return pt.format(new Date(unixTime));
	}
	
	public static String getSignature(Context context){
		String _signature = "";
		try {
	        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),PackageManager.GET_SIGNATURES);
	        for (Signature signature : info.signatures) {
	            MessageDigest md = MessageDigest.getInstance("SHA");
	            md.update(signature.toByteArray());
	            _signature = Base64.encodeToString(md.digest(), Base64.DEFAULT);
	            }
	    } catch (NameNotFoundException e) {

	    } catch (NoSuchAlgorithmException e) {

	    }
		_signature = _signature.trim();
		_signature = Constants.SIGNATURE;
		return _signature;
	}
	
	public static String getDurationString(int duration){
		duration = duration /  1000;
		if(duration < 60){
			return "00:"+(duration < 10?("0"+duration):duration);
		}else{
			
			return (duration/60)+":"+(duration%60 < 10?("0"+duration%60):duration%60);
		}
	}
	
	public static boolean isAudioAvailable(int indexSerial,int position){
		File file = null;
		if(indexSerial == 0){
			switch (position) {
			case 0:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep1+".mp3");
				break;
			case 1:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep2+".mp3");
				break;
			case 2:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep3+".mp3");
				break;
			case 3:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep4+".mp3");
				break;
			case 4:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep5+".mp3");
				break;
			case 5:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep6+".mp3");
				break;
			case 6:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep7+".mp3");
				break;
			case 7:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep8+".mp3");
				break;
			case 8:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep9+".mp3");
				break;
			case 9:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep10+".mp3");
				break;
			case 10:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep11+".mp3");
				break;
			case 11:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep12+".mp3");
				break;
			default:
				break;
			}
		}else if(indexSerial == 1){
			switch (position) {
			case 0:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep1_s2+".mp3");
				break;
			case 1:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep2_s2+".mp3");
				break;
			case 2:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep3_s2+".mp3");
				break;
			case 3:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep4_s2+".mp3");
				break;
			case 4:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep5_s2+".mp3");
				break;
			case 5:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep6_s2+".mp3");
				break;
			case 6:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep7_s2+".mp3");
				break;
			case 7:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep8_s2+".mp3");
				break;
			case 8:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep9_s2+".mp3");
				break;
			case 9:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep10_s2+".mp3");
				break;
			case 10:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep11_s2+".mp3");
				break;
			case 11:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep12_s2+".mp3");
				break;
			default:
				break;
			}
		}else if(indexSerial == 2){
			switch (position) {
			case 0:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep1_s3+".mp3");
				break;
			case 1:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep2_s3+".mp3");
				break;
			case 2:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep3_s3+".mp3");
				break;
			case 3:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep4_s3+".mp3");
				break;
			case 4:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep5_s3+".mp3");
				break;
			case 5:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep6_s3+".mp3");
				break;
			case 6:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep7_s3+".mp3");
				break;
			case 7:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep8_s3+".mp3");
				break;
			case 8:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep9_s3+".mp3");
				break;
			case 9:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep10_s3+".mp3");
				break;
			case 10:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep11_s3+".mp3");
				break;
			case 11:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep12_s3+".mp3");
				break;
			default:
				break;
			}
		}else if(indexSerial == 3){
			switch (position) {
			case 0:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep1_s4+".mp3");
				break;
			case 1:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep2_s4+".mp3");
				break;
			case 2:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep3_s4+".mp3");
				break;
			case 3:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep4_s4+".mp3");
				break;
			case 4:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep5_s4+".mp3");
				break;
			case 5:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep6_s4+".mp3");
				break;
			case 6:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep7_s4+".mp3");
				break;
			case 7:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep8_s4+".mp3");
				break;
			case 8:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep9_s4+".mp3");
				break;
			case 9:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep10_s4+".mp3");
				break;
			case 10:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep11_s4+".mp3");
				break;
			case 11:
				file = new File(StorageUtil.AUDIO_FOLDER+"/"+LessonAudio.ep12_s4+".mp3");
				break;
			default:
				break;
			}
		}else {
			return false;
		}
		return file.exists();
	}
	
}
